//
//  RJWarningFirstCheckView.m
//  AirQuality
//
//  Created by Po on 15/4/14.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningFirstCheckView.h"
#import "RJWarningFirstCheckDay.h"
#import "RJTools.h"


@interface RJWarningFirstCheckView () <RJWarningFatherDeleaget>

@property (retain, nonatomic) NSMutableArray * daysArray;

- (void)initInterface;

@end

@implementation RJWarningFirstCheckView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
        [self setFatherDelegate:self];
    }
    return self;
}

- (void)initInterface
{
    _daysArray = [[NSMutableArray alloc] init];
    NSUInteger dayCount = 1;
    for (NSUInteger i = 0; i < 3 ; i ++) {
        RJWarningFirstCheckDay * day = [[RJWarningFirstCheckDay alloc] initWithFrame:CGRectMake(0, 210 *  i, ScreenWidth, 200)];
        [day.timeLabel setText:[RJTools getTimeWithFormat:@"MM月dd日" time:GetFutureSinceNow(dayCount)]];
        [self addSubview:day];
        [_daysArray addObject:day];
        dayCount ++;
    }
}

#pragma mark - function
- (NSArray *)getDaysDetailWithData:(NSArray *)data
{
    NSMutableArray * daysDetailArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 3; i ++) {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        for (NSDictionary * dic in data) {
            NSString * firstThing = [NSString stringWithFormat:@"首要污染物 - %@",dic[@"PRIMARYPOLLUTANT"][i]];
            NSString * rank = [NSString stringWithFormat:@"污染等级 - %@",dic[@"QUALITY"][i]];
            [array addObject:@{dic[@"USERNAME"]:@[firstThing,rank]}];
        }
        [daysDetailArray addObject:array];
    }
    return daysDetailArray;
}

#pragma mark - WarningFatherDelegate
- (void)WarningFatherSetData:(NSArray *)data
{
    if ([RJUserData defaultRJUserData].isDone || [RJUserData defaultRJUserData].isAudit) {
        if (data.count == 0) {
            return;
        }
        for (NSUInteger i = 0; i < 3; i ++) {
            RJWarningFirstCheckDay * day = _daysArray[i];
            day.detailData = [self getDaysDetailWithData:data[0]][i];
            if ([RJUserData defaultRJUserData].isAudit) {
                [day.selectLabel setText:data[1][i]];
                [day.aqiRankLabel setText:data[2][i]];
            }
        }
    }
}

- (void)WarningFatherSetUserData:(NSDictionary *)data
{
    if ([data count] == 0) {
        return;
    }
    NSArray * mainClassData = data[@"aqi_audit"];
    NSArray * primaryPollutants = data[@"primaryPollutant_audit"];
//    NSArray * quality = data[@"quality_audit"];
    
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningFirstCheckDay * day = _daysArray[i];
        //计算等级
        if (mainClassData && [mainClassData count] != 0 && mainClassData[i]) {
            [day.aqiRankLabel setText:[self getRankWithNumber:mainClassData[i]]];
        }
        
        if ([primaryPollutants count] == 0) {
            continue;
        }
        
        //设置选项
        if (primaryPollutants && [primaryPollutants count] != 0) {
            if (![primaryPollutants[i] isEqualToString:@""] && ![primaryPollutants[i] isEqualToString:@"点击选择"]) {
                [day.selectLabel setText:primaryPollutants[i]];
            } else {
                [day.selectLabel setText:@"点击选择"];
            }
        }
    }
}

- (void)WarningFatherWillRemoveSelf
{}

- (NSDictionary *)WarningFatherGetData
{
    NSMutableArray * primaryPollutants = [NSMutableArray array];
    NSMutableArray * qualitys = [NSMutableArray array];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningFirstCheckDay * day = _daysArray[i];
        [primaryPollutants addObject:day.selectLabel.text];
        [qualitys addObject:day.aqiRankLabel.text];
    }
    return @{@"primaryPollutant_audit":primaryPollutants,@"quality_audit":qualitys,@"message":@""};
}

- (NSString *)getRankWithNumber:(NSString *)aqiRank
{
    if ([aqiRank floatValue] == 0) {
        return @"";
    }
    NSRange aqiRankRange = [aqiRank rangeOfString:@"~"];
    NSMutableString * rankString = [[RJTools getRankWithNumber:[[aqiRank substringToIndex:aqiRankRange.location] floatValue]] mutableCopy];
    NSString * maxRange = [RJTools getRankWithNumber:[[aqiRank substringFromIndex:aqiRankRange.location + 1] floatValue]];
    if (![rankString isEqualToString:maxRange]) {
        [rankString appendFormat:@"或%@",maxRange];
    }
    return rankString;
}
@end
